package scau.erps.view.business.team;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;







import scau.erps.app.Erps;
import scau.erps.entity.Incomestatement;
import scau.erps.entity.Sand;
import scau.erps.service.IncomestatementService;
import scau.erps.service.SandService;
import scau.erps.service.impl.IncomestatementServiceImpl;


import scau.erps.service.impl.SandServiceImpl;

/**
 * 损益表面板
 * @author FYJ
 * 
 */
public class MyProfitLossPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	protected JComboBox<String> teamCombo;
	protected JButton searchBtn, operationBtn1, operationBtn2, operationBtn3,
			operationBtn4;
	protected JPanel contentPanel, actionPanel;
	protected JScrollPane dataPanel;
	protected JTable table;
	protected DefaultTableCellRenderer render;
	private OrderTableModel2 dataModel;
	final int team_Id = Erps.getCurrentTeam().getId();
	SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);

	public MyProfitLossPanel() {
		teamCombo = new JComboBox<String>(new String[]{"所有年份","Y0","Y1","Y2","Y3","Y4","Y5","Y6","Y7"});
		searchBtn = new JButton("显示今年年损益表");
		operationBtn1 = new JButton("查询选中年份损益表"); 
		searchBtn.addActionListener(this);
		operationBtn1.addActionListener(this);
		actionPanel = new JPanel(); // 顶部交互操作面板
		actionPanel.add(searchBtn);
		actionPanel.add(teamCombo);
		actionPanel.add(operationBtn1);
		table = new JTable();
		table.setPreferredScrollableViewportSize(new Dimension(930, 500));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		dataPanel = new JScrollPane(table);
		dataPanel.setViewportView(table);
		render = new DefaultTableCellRenderer();
		render.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, render);
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(actionPanel, BorderLayout.NORTH);
		contentPanel.add(dataPanel, BorderLayout.CENTER);
		this.add(contentPanel);
	}

	private OrderTableModel2 getOrderTableModel() {
		Sand sand = sandService.getSandByTeam(team_Id);
		String y =sand.getTime().charAt(1) + "";
		dataModel = new OrderTableModel2(y);
		return dataModel;

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchBtn) {//查询当年损益表
			dataModel = getOrderTableModel();
			this.table.setModel(dataModel);
		}
		else if(e.getSource() == operationBtn1){//查询所选年份损益表
			String year = null;
			String y = null;
			if (teamCombo.getSelectedIndex() != 0) {
				year = (String) teamCombo.getSelectedItem();
				y = year.charAt(1)+"";
				dataModel = new OrderTableModel2(y);
				this.table.setModel(dataModel);
			}
			else
				JOptionPane.showMessageDialog(null, "请选择需要查询年份", "提示",
						JOptionPane.INFORMATION_MESSAGE);
		}
	}
}

class OrderTableModel2 extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] title = { "项目", "去年", "期末数" };
	private Object[][] data = null;
	private int saleIncome = 0, cost = 0, grossMargin = 0,
			comprehensiveCost = 0, depreciation = 0, financial = 0,
			operatingProfit = 0, extraProfit = 0, allProfit = 0, incomeTax = 0,
			netProfit = 0;// 当年损益表数据
	private int temp = 40, temp1 = 17, temp2 = 23, temp3 = 8, temp4 = 4,
			temp5 = 1, temp6 = 10, temp7 = 0, temp8 = 10, temp9 = 3, temp10 = 7;// 缓存去年损益表数据
	private int team_Id = Erps.getCurrentTeam().getId();
	IncomestatementService incomestatementService = Erps.getIocInstance().get(
			IncomestatementServiceImpl.class);
	public OrderTableModel2() {

	}

	public OrderTableModel2(String y) {
		List<Incomestatement> thisIncomestatementList = incomestatementService
				.getIncomestatementOfThisYear(team_Id, "Y" + y);
		Incomestatement thisIncomestatement = thisIncomestatementList.get(0);
		if (y.equals("0"))
				y = "9";
			List<Incomestatement> okdIncomestatementList = incomestatementService
					.getIncomestatementOfLastYear(team_Id, "Y" + y);
			Incomestatement okdIncomestatement = okdIncomestatementList.get(0);
			temp = okdIncomestatement.getIncome();
			temp1 = okdIncomestatement.getCost();
			temp2 = okdIncomestatement.getGrossprofit();
			temp3 = okdIncomestatement.getComprehensiveFee();
			temp4 = okdIncomestatement.getDepreciation();
			temp5 = okdIncomestatement.getNet_profit_loss();
			temp6 = okdIncomestatement.getProfit();
			temp7 = okdIncomestatement.getExtroIncome();
			temp8 = okdIncomestatement.getTotalProfit();
			temp9 = okdIncomestatement.getTax();
			temp10 = okdIncomestatement.getNetProfit();// 去年数据
		// 获取当年已收款订单的总收入额
		saleIncome = thisIncomestatement.getIncome();
		// 获取生产成本
		cost = thisIncomestatement.getCost();
		grossMargin = thisIncomestatement.getGrossprofit();// 算出毛利
		// 获取综合费用
		comprehensiveCost = thisIncomestatement.getComprehensiveFee();
		depreciation = thisIncomestatement.getDepreciation();// 算取当年生产线折旧费用
		financial = thisIncomestatement.getNet_profit_loss();// 获取当年财务净损益
		operatingProfit = thisIncomestatement.getProfit();// 算取营业利润
		extraProfit = thisIncomestatement.getExtroIncome();// 营业外收入
		allProfit = thisIncomestatement.getTotalProfit();// 利润总额
		incomeTax = thisIncomestatement.getTax();// 个人所得税							
		netProfit = thisIncomestatement.getNetProfit();// 算取净利润

		data = new Object[11][title.length];
		data[0][0] = "一、销售收入";
		data[0][1] = temp;
		data[0][2] = saleIncome;
		data[1][0] = "减：成本";
		data[1][1] = temp1;
		data[1][2] = cost;
		data[2][0] = "二、毛利";
		data[2][1] = temp2;
		data[2][2] = grossMargin;
		data[3][0] = "减：综合费用";
		data[3][1] = temp3;
		data[3][2] = comprehensiveCost;
		data[4][0] = "   折旧";
		data[4][1] = temp4;
		data[4][2] = depreciation;
		data[5][0] = "   财务净损益";
		data[5][1] = temp5;
		data[5][2] = financial;
		data[6][0] = "三、营业利润";
		data[6][1] = temp6;
		data[6][2] = operatingProfit;
		data[7][0] = "加：营业外净收入";
		data[7][1] = temp7;
		data[7][2] = extraProfit;
		data[8][0] = "四、利润总额";
		data[8][1] = temp8;
		data[8][2] = allProfit;
		data[9][0] = "减：所得税";
		data[9][1] = temp9;
		data[9][2] = incomeTax;
		data[10][0] = "五、净利润";
		data[10][1] = temp10;
		data[10][2] = netProfit;//显示损益表
	}

	@Override
	public int getRowCount() {
		return this.data.length; // 行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; // 列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex]; // 取表格单元格值
	}

	public String getColumnName(int columnIndex) {
		return title[columnIndex]; // 显示标题
	}

}
